﻿using System;
using System.Collections.Generic;
using System.IO;

namespace Net.SceneUtility
{
    /// <summary>
    /// 地图九宫格初始化数据
    /// </summary>
    [Serializable]
    public class MapAOIData
    {
        /// <summary>
        /// 网格偏移
        /// </summary>
        public Vector2 offset;
        /// <summary>
        /// 格子列大小
        /// </summary>
        public Vector2 cellSize;
        /// <summary>
        /// 网格世界大小
        /// </summary>
        public Vector2 worldSize;
    }

    /// <summary>
    /// 地图数据
    /// </summary>
    [Serializable]
    public class MapData
    {
        /// <summary>
        /// 地图名称
        /// </summary>
        public string name;
        /// <summary>
        /// 地图的所有怪物点
        /// </summary>
        public List<MapMonsterPoint> monsterPoints = new();
        /// <summary>
        ///  简单在路径点，可以用作服务器NPC位置，玩家出生位置等等
        /// </summary>
        public List<RoamingPathData> roamingPaths = new();
        /// <summary>
        /// 地图九宫格数据
        /// </summary>
        public MapAOIData aoiData = new();
        /// <summary>
        /// 寻路网格文件路径
        /// </summary>
        public byte[] navmeshData;

        /// <summary>
        /// 读取地图数据
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static MapData ReadFileData(string path)
        {
            var jsonStr = File.ReadAllText(path);
            return ReadData(jsonStr);
        }

        public static MapData ReadData(string jsonStr)
        {
            return Newtonsoft_X.Json.JsonConvert.DeserializeObject<MapData>(jsonStr);
        }

        /// <summary>
        /// 写入地图数据 --只有unity编辑器使用
        /// </summary>
        /// <param name="path"></param>
        public static void WriteData(string path)
        {
#if UNITY_EDITOR
            var scene = UnityEngine.SceneManagement.SceneManager.GetActiveScene();
            var roamingPaths = UnityEngine.Object.FindObjectsByType<RoamingPath>(UnityEngine.FindObjectsSortMode.None);
            var sceneData = new MapData
            {
                name = scene.name
            };
            foreach (var item in roamingPaths)
            {
                var monsterPoint = item.GetComponent<MonsterPoint>();
                if (monsterPoint != null)
                {
                    sceneData.monsterPoints.Add(new MapMonsterPoint()
                    {
                        name = item.name,
                        patrolPath = new RoamingPathData() { name = item.Name, waypoints = item.WorldPointList },
                        monsters = monsterPoint.monsters,
                    });
                }
                else
                {
                    sceneData.roamingPaths.Add(new RoamingPathData() { name = item.Name, waypoints = item.WorldPointList });
                }
            }
            var aoiMgr = UnityEngine.Object.FindFirstObjectByType<Component.AOIManager>();
            if (aoiMgr != null)
            {
                sceneData.aoiData = new MapAOIData()
                {
                    offset = aoiMgr.offset,
                    cellSize = aoiMgr.cellSize,
                    worldSize = aoiMgr.worldSize,
                };
            }
            var navmeshSystemUnityType = Helper.AssemblyHelper.GetTypeNotOptimized("Net.AI.NavmeshSystemUnity");
            if (navmeshSystemUnityType != null)
            {
                var navmesh = UnityEngine.Object.FindFirstObjectByType(navmeshSystemUnityType);
                if (navmesh != null)
                {
                    var navmeshPathProperty = navmeshSystemUnityType.GetProperty("NavmeshPath");
                    if (navmeshPathProperty != null)
                    {
                        var navmeshPath = navmeshPathProperty.GetValue(navmesh) as string;
                        if (File.Exists(navmeshPath))
                            sceneData.navmeshData = File.ReadAllBytes(navmeshPath);
                    }
                }
            }
            var jsonStr = Newtonsoft_X.Json.JsonConvert.SerializeObject(sceneData);
            var dirPath = Path.GetDirectoryName(path);
            if (!Directory.Exists(dirPath))
                Directory.CreateDirectory(dirPath);
            File.WriteAllText(path, jsonStr);
            UnityEngine.Debug.Log($"地图数据生成成功: {new DirectoryInfo(path).FullName}");
#else
            Net.Event.NDebug.LogError("只有Unity编辑器能用!!!");
#endif
        }
    }
}